﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;


namespace Gui.Functies
{
    public partial class Takenlijst : UserControl
    {
        int projectID;

        public Takenlijst()
        {
            InitializeComponent();
            getTaken();
        }

        public Takenlijst(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getTaken();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form f1 = new Takenlijst_popups.Addtaak(projectID);
            f1.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form f3 = new Takenlijst_popups.Removetaak(projectID);
            f3.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form f2 = new Takenlijst_popups.Addlid(projectID);
            f2.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form f4 = new Takenlijst_popups.Edittaak(projectID);
            f4.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            getTaken();
        }
        
        public void getTaken()
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand takenGet = new MySqlCommand("SELECT TaakID, Taak, Status FROM taken WHERE ProjectID='" + projectID + "' ORDER BY TaakID ASC", grabConnection);

                //takenGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();

                MySqlDataReader reader = takenGet.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        takenGet.Dispose();
                    }
                    else
                    {
                        listView1.Items.Clear();

                        while (reader.Read())
                        {
                            ListViewItem item = new ListViewItem(reader["Taak"].ToString());
                            item.SubItems.Add(reader["TaakID"].ToString()); // hier moet naam komen
                            item.SubItems.Add(reader["Status"].ToString());

                            listView1.Items.Add(item);
                        }

                        reader.Close();
                    }

                    grabConnection.Close();
                    reader.Dispose();
                    takenGet.Dispose();
                }
            }

            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
}
